package com.hsgene.order.listener;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.Message;
import com.fasterxml.jackson.core.type.TypeReference;
import com.hsgene.common.util.basic.JsonUtils;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.order.dto.message.OrderPostponeMessageDto;
import com.hsgene.order.service.OrderPostponeService;
import com.hsgene.util.mq.rocket.annotation.RocketMQListener;
import com.hsgene.util.mq.rocket.consumer.AbstractRocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * RocketMQ 超期检测消息监听
 *
 * @author wangbing
 * @version 1.0
 * @date 2019/1/21 14:55
 * Copyright: Copyright (c) 2019
 */
@RocketMQListener(topic = "genetic_testing_order", tag = "genetic_testing_order_postpone")
public class OrderPostponeListener extends AbstractRocketMQListener {

    private final static Logger LOGGER = LoggerFactory.getLogger(OrderPostponeListener.class);

    @Resource
    private OrderPostponeService orderPostponeService;

    @Override
    public Action messageHandler(Message message) {
        String data = new String(message.getBody());
        OrderPostponeMessageDto msg;
        try {
            msg = JsonUtils.jsonToObject(data, new TypeReference<OrderPostponeMessageDto>() {
            });
        } catch (Exception e) {
            LOGGER.error("消息数据格式有误，无法进行json格式化解析：" + data);
            return Action.ReconsumeLater;
        }
        ApiResult<NullObject> handleResult = orderPostponeService.handlePostponeMsg(msg);
        if (handleResult.isSucc()) {
            return Action.CommitMessage;
        }
        LOGGER.error("超期检测处理失败！message = {} , MSGBody = {} , error = {}", message, data, handleResult.getDesc());
        return Action.ReconsumeLater;
    }
}
